
#include <replace_panel.h>
#include "utils.h"
#include "doc_panel_base.h"




static glong string_get_length (const char* self);
enum  {
	REPLACE_PANEL_DUMMY_PROPERTY,
	REPLACE_PANEL_REPLACE_PHARSE
};
static gboolean _replace_panel_on_replace_gtk_widget_key_press_event (GtkEntry* _sender, const GdkEventKey* event, gpointer self);
static void _replace_panel_on_replace_pharse_entry_changed_gtk_editable_changed (GtkEntry* _sender, gpointer self);
static gboolean replace_panel_real_on_search (SearchPanel* base, GtkEntry* sender, const GdkEventKey* event);
static gboolean replace_panel_real_on_replace (ReplacePanel* self, GtkEntry* sender, const GdkEventKey* event);
static gpointer replace_panel_parent_class = NULL;
static void replace_panel_finalize (GObject* obj);
static int _vala_strcmp0 (const char * str1, const char * str2);



static glong string_get_length (const char* self) {
	g_return_val_if_fail (self != NULL, 0L);
	return g_utf8_strlen (self, -1);
}


static gboolean _replace_panel_on_replace_gtk_widget_key_press_event (GtkEntry* _sender, const GdkEventKey* event, gpointer self) {
	return replace_panel_on_replace (self, _sender, event);
}


static void _replace_panel_on_replace_pharse_entry_changed_gtk_editable_changed (GtkEntry* _sender, gpointer self) {
	replace_panel_on_replace_pharse_entry_changed (self);
}


ReplacePanel* replace_panel_construct (GType object_type, GtkSourceBuffer** source_buff, GtkSourceView** source_view) {
	ReplacePanel * self;
	GtkLabel* replace_pharse_entry_lbl;
	GtkEntry* _tmp0;
	g_return_val_if_fail (source_buff != NULL, NULL);
	g_return_val_if_fail (source_view != NULL, NULL);
	self = (ReplacePanel*) search_panel_construct (object_type, &(*source_buff), &(*source_view));
	replace_pharse_entry_lbl = g_object_ref_sink ((GtkLabel*) gtk_label_new_with_mnemonic ("<b>_Zastąp:</b>"));
	gtk_label_set_use_markup (replace_pharse_entry_lbl, TRUE);
	_tmp0 = NULL;
	self->replace_pharse_entry = (_tmp0 = g_object_ref_sink ((GtkEntry*) gtk_entry_new ()), (self->replace_pharse_entry == NULL) ? NULL : (self->replace_pharse_entry = (g_object_unref (self->replace_pharse_entry), NULL)), _tmp0);
	gtk_label_set_mnemonic_widget (replace_pharse_entry_lbl, (GtkWidget*) self->replace_pharse_entry);
	gtk_table_attach ((GtkTable*) self, (GtkWidget*) replace_pharse_entry_lbl, (guint) 3, (guint) 4, (guint) 0, (guint) 1, GTK_SHRINK, GTK_SHRINK, (guint) 5, (guint) 0);
	gtk_table_attach_defaults ((GtkTable*) self, (GtkWidget*) self->replace_pharse_entry, (guint) 4, (guint) 5, (guint) 0, (guint) 1);
	g_signal_connect_object ((GtkWidget*) self->replace_pharse_entry, "key-press-event", (GCallback) _replace_panel_on_replace_gtk_widget_key_press_event, self, 0);
	g_signal_connect_object ((GtkEditable*) self->replace_pharse_entry, "changed", (GCallback) _replace_panel_on_replace_pharse_entry_changed_gtk_editable_changed, self, 0);
	if (gtk_text_tag_table_lookup (gtk_text_buffer_get_tag_table ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff), "selection") == NULL) {
		GtkTextTag* selection_tag;
		GdkColor c;
		selection_tag = gtk_text_buffer_create_tag ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, "selection", NULL);
		c = gtk_widget_get_style ((GtkWidget*) ((DocPanelBase*) self)->source_view)->base[0];
		utils_neg_color (&c);
		g_object_set (selection_tag, "background", gdk_color_to_string (&c), NULL);
		c = gtk_widget_get_style ((GtkWidget*) ((DocPanelBase*) self)->source_view)->fg[0];
		utils_neg_color (&c);
		g_object_set (selection_tag, "foreground", gdk_color_to_string (&c), NULL);
	}
	return self;
}


ReplacePanel* replace_panel_new (GtkSourceBuffer** source_buff, GtkSourceView** source_view) {
	return replace_panel_construct (TYPE_REPLACE_PANEL, source_buff, source_view);
}


static gboolean replace_panel_real_on_search (SearchPanel* base, GtkEntry* sender, const GdkEventKey* event) {
	ReplacePanel * self;
	gboolean _tmp0;
	self = (ReplacePanel*) base;
	g_return_val_if_fail (sender != NULL, FALSE);
	_tmp0 = FALSE;
	if (_vala_strcmp0 (gdk_keyval_name ((*event).keyval), "Tab") == 0) {
		_tmp0 = TRUE;
	} else {
		_tmp0 = _vala_strcmp0 (gdk_keyval_name ((*event).keyval), "Return") == 0;
	}
	if (_tmp0) {
		if (string_get_length (search_panel_get_search_pharse ((SearchPanel*) self)) == 0) {
			utils_set_widget_warrnig_background (&((SearchPanel*) self)->pharse_entry);
		} else {
			gtk_widget_grab_focus ((GtkWidget*) self->replace_pharse_entry);
		}
		return TRUE;
	}
	return FALSE;
}


static gboolean replace_panel_real_on_replace (ReplacePanel* self, GtkEntry* sender, const GdkEventKey* event) {
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (sender != NULL, FALSE);
	if (_vala_strcmp0 (gdk_keyval_name ((*event).keyval), "Return") == 0) {
		if (string_get_length (replace_panel_get_replace_pharse (self)) == 0) {
			utils_set_widget_warrnig_background (&self->replace_pharse_entry);
			if (string_get_length (search_panel_get_search_pharse ((SearchPanel*) self)) == 0) {
				utils_set_widget_warrnig_background (&((SearchPanel*) self)->pharse_entry);
				gtk_widget_grab_focus ((GtkWidget*) ((SearchPanel*) self)->pharse_entry);
			}
			return TRUE;
		}
		SEARCH_PANEL_CLASS (replace_panel_parent_class)->on_search (SEARCH_PANEL (self), sender, &(*event));
		if (g_list_length (((SearchPanel*) self)->founded) > 0) {
			do {
				GtkTextIter start_pos = {0};
				GtkTextIter end_pos = {0};
				GtkMessageDialog* msg;
				gint response;
				gtk_text_buffer_get_iter_at_mark ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &start_pos, (GtkTextMark*) g_list_nth_data (((SearchPanel*) self)->founded, (guint) ((SearchPanel*) self)->founded_pos));
				gtk_text_buffer_get_iter_at_mark ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &end_pos, (GtkTextMark*) g_list_nth_data (((SearchPanel*) self)->founded, (guint) (((SearchPanel*) self)->founded_pos + 1)));
				gtk_text_buffer_apply_tag_by_name ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, "selection", &start_pos, &end_pos);
				msg = g_object_ref_sink ((GtkMessageDialog*) gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "Czy chcesz zastąpić zaznaczone tekst ?", NULL));
				gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_YES, (gint) GTK_RESPONSE_YES);
				gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_NO, (gint) GTK_RESPONSE_NO);
				gtk_dialog_add_button ((GtkDialog*) msg, GTK_STOCK_CANCEL, (gint) GTK_RESPONSE_CANCEL);
				gtk_dialog_set_default_response ((GtkDialog*) msg, (gint) GTK_RESPONSE_YES);
				response = gtk_dialog_run ((GtkDialog*) msg);
				gtk_object_destroy ((GtkObject*) msg);
				gtk_text_buffer_remove_tag_by_name ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, "selection", &start_pos, &end_pos);
				if (response == GTK_RESPONSE_YES) {
					gtk_text_buffer_delete ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &start_pos, &end_pos);
					/*Zmienil sie tekst, uaktualniamy itera...*/
					gtk_text_buffer_get_iter_at_mark ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &start_pos, (GtkTextMark*) g_list_nth_data (((SearchPanel*) self)->founded, (guint) ((SearchPanel*) self)->founded_pos));
					gtk_text_buffer_insert ((GtkTextBuffer*) ((DocPanelBase*) self)->source_buff, &start_pos, replace_panel_get_replace_pharse (self), (gint) string_get_length (replace_panel_get_replace_pharse (self)));
				} else {
					if (response == GTK_RESPONSE_CANCEL) {
						gboolean _tmp1;
						doc_panel_base_close ((DocPanelBase*) self);
						return (_tmp1 = TRUE, (msg == NULL) ? NULL : (msg = (g_object_unref (msg), NULL)), _tmp1);
					}
				}
				search_panel_goto_next_match ((SearchPanel*) self);
				(msg == NULL) ? NULL : (msg = (g_object_unref (msg), NULL));
			} while (((SearchPanel*) self)->founded_pos < (g_list_length (((SearchPanel*) self)->founded) - 2));
		} else {
			gtk_widget_grab_focus ((GtkWidget*) ((SearchPanel*) self)->pharse_entry);
		}
	}
	return FALSE;
}


gboolean replace_panel_on_replace (ReplacePanel* self, GtkEntry* sender, const GdkEventKey* event) {
	return REPLACE_PANEL_GET_CLASS (self)->on_replace (self, sender, event);
}


void replace_panel_on_replace_pharse_entry_changed (ReplacePanel* self) {
	g_return_if_fail (self != NULL);
	gtk_widget_modify_base ((GtkWidget*) self->replace_pharse_entry, GTK_STATE_NORMAL, NULL);
}


const char* replace_panel_get_replace_pharse (ReplacePanel* self) {
	g_return_val_if_fail (self != NULL, NULL);
	return gtk_entry_get_text (self->replace_pharse_entry);
}


void replace_panel_set_replace_pharse (ReplacePanel* self, const char* value) {
	g_return_if_fail (self != NULL);
	gtk_entry_set_text (self->replace_pharse_entry, value);
	g_object_notify ((GObject *) self, "replace-pharse");
}


static void replace_panel_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	ReplacePanel * self;
	gpointer boxed;
	self = REPLACE_PANEL (object);
	switch (property_id) {
		case REPLACE_PANEL_REPLACE_PHARSE:
		g_value_set_string (value, replace_panel_get_replace_pharse (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void replace_panel_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	ReplacePanel * self;
	self = REPLACE_PANEL (object);
	switch (property_id) {
		case REPLACE_PANEL_REPLACE_PHARSE:
		replace_panel_set_replace_pharse (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void replace_panel_class_init (ReplacePanelClass * klass) {
	replace_panel_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->get_property = replace_panel_get_property;
	G_OBJECT_CLASS (klass)->set_property = replace_panel_set_property;
	G_OBJECT_CLASS (klass)->finalize = replace_panel_finalize;
	SEARCH_PANEL_CLASS (klass)->on_search = replace_panel_real_on_search;
	REPLACE_PANEL_CLASS (klass)->on_replace = replace_panel_real_on_replace;
	g_object_class_install_property (G_OBJECT_CLASS (klass), REPLACE_PANEL_REPLACE_PHARSE, g_param_spec_string ("replace-pharse", "replace-pharse", "replace-pharse", NULL, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void replace_panel_instance_init (ReplacePanel * self) {
}


static void replace_panel_finalize (GObject* obj) {
	ReplacePanel * self;
	self = REPLACE_PANEL (obj);
	(self->replace_pharse_entry == NULL) ? NULL : (self->replace_pharse_entry = (g_object_unref (self->replace_pharse_entry), NULL));
	G_OBJECT_CLASS (replace_panel_parent_class)->finalize (obj);
}


GType replace_panel_get_type (void) {
	static GType replace_panel_type_id = 0;
	if (replace_panel_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (ReplacePanelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) replace_panel_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ReplacePanel), 0, (GInstanceInitFunc) replace_panel_instance_init, NULL };
		replace_panel_type_id = g_type_register_static (TYPE_SEARCH_PANEL, "ReplacePanel", &g_define_type_info, 0);
	}
	return replace_panel_type_id;
}


static int _vala_strcmp0 (const char * str1, const char * str2) {
	if (str1 == NULL) {
		return -(str1 != str2);
	}
	if (str2 == NULL) {
		return str1 != str2;
	}
	return strcmp (str1, str2);
}




